package com.jz.pay.bo.impl;

import com.jz.pay.bo.RedisTemplateBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.scripting.support.StaticScriptSource;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.Serializable;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

@Component
public class RedisTemplateBOImpl implements RedisTemplateBO {

    @Autowired(required = false)
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private JedisPool jedisPool;

    @Override
    public void setString(String key, String value) {
        String redisKey = buildKey(key);
//        redisTemplate.opsForValue().set(redisKey,value);
        jedisPool.getResource().set(redisKey,value,"NX");
    }

    @Override
    public void setString(String key, String value, long timeout) {
        setString(key,value,timeout,TimeUnit.MILLISECONDS);
    }

    @Override
    public void setString(String key, String value, long timeout, TimeUnit timeUnit) {
        String redisKey = buildKey(key);
//        redisTemplate.opsForValue().set(redisKey,value,timeout,timeUnit);
        // NX是不存在时才set， XX是存在时才set， EX是秒，PX是毫秒
        jedisPool.getResource().set(redisKey,value,"NX","EX",timeout);
    }

    @Override
    public String getString(String key) {
        String redisKey = buildKey(key);
//        Serializable serializable = redisTemplate.opsForValue().get(redisKey);
        String value = jedisPool.getResource().get(redisKey);
        return value ;
//        if(serializable == null){
//            return null;
//        }
//        return serializable.toString();
    }

    @Override
    public void setObject(String key, Object value) {
        String redisKey = buildKey(key);
    }

    @Override
    public void setObject(String key, Object value, long timeout) {
        setObject(key,value,timeout,TimeUnit.MILLISECONDS);
    }

    @Override
    public void setObject(String key, Object value, long timeout, TimeUnit timeUnit) {

    }

    @Override
    public String getObject(String key) {
        return null;
    }

    @Override
    public boolean exist(String key) {
        String redisKey = buildKey(key);
//        Serializable serializable = redisTemplate.opsForValue().get(redisKey);
        Boolean exists = jedisPool.getResource().exists(redisKey);
        return exists;

    }

    @Override
    public Long del(String key) {
        String redisKey = buildKey(key);
//        Serializable serializable = redisTemplate.opsForValue().get(redisKey);
        Long result = jedisPool.getResource().del(key);
        return result;
    }

    @Override
    public void hashIncre(String key, String property) {
//        String script = "if redis.call('HEXISTS', KEYS[1], ARGV[1]) == 1 then\n" +
//                "   return redis.call('HINCRBY', KEYS[1], ARGV[1], 1)\n" +
//                "else\n" +
//                "   return redis.call('HSET', KEYS[1], ARGV[1], 1)\n" +
//                "end";
//
//        DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();
//        redisScript.setScriptSource(new StaticScriptSource(script));
//        redisScript.setResultType(Long.class);
        // Execute Lua script
//        Long result = redisTemplate.execute(redisScript, Collections.singletonList(key), property);
        hashIncre(key,property,1);
    }
    @Override
    public void hashIncre(String key, String property,long increment) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            Long result = jedis.hincrBy(key,property,increment);
            System.out.println("lua结果："+result);
        }catch (Exception e){
            throw e;
        }finally {
            jedis.close();
//            System.out.println("lua结果："+result);
        }
    }


    private String buildKey(String key){
        return key;
    }

}
